RAIS  3.2
C:/Projekte/RAIS/InstallRAISDB/Installer1.cs
Go to the documentation of this file.
00001 using System;
00002 using System.Collections;
00003 using System.Collections.Generic;
00004 using System.ComponentModel;
00005 using System.Configuration.Install;
00006 using System.Linq;
00007 using System.IO;
00008 using System.Data.SqlClient;
00009 using System.Xml;
00010 using Microsoft.Win32;
00011 using System.IO.Compression;
00012 using System.Windows.Forms;
00013 
00014 namespace InstallRAISDB
00015 {
00016     [RunInstaller(true)]
00017     public partial class Installer1 : Installer
00018     {
00019         TextWriter log;
00020         string connectionString;
00021         IDictionary _savedState;
00022 
00023         public Installer1()
00024         {
00025             InitializeComponent();
00026         }
00027         protected override void OnAfterInstall(IDictionary savedState)
00028         {
00029             _savedState = savedState;
00030             base.OnAfterInstall(savedState);
00031 
00032             using (FileStream file = File.Open("c:\\log.txt", FileMode.OpenOrCreate))
00033             {
00034                 if (File.Exists(Context.Parameters["targetdir"] + "\\flag"))
00035                 {
00036                     var result = MessageBox.Show("It seems that there is already an installation of RAIS on this machine. \r\n\r\n If you continue, an existing Database could be overwritten! \r\n\r\n Do you want to continue ?  ", "Caution",
00037                                     MessageBoxButtons.YesNo,
00038                                     MessageBoxIcon.Question);
00039 
00040                     if (result == DialogResult.No)
00041                     {
00042                         MessageBox.Show("RAIS Installation will finish, but NO database will be installed ! ", "Caution",
00043                                     MessageBoxButtons.OK,
00044                                     MessageBoxIcon.Information);
00045                         return;
00046                     }
00047                 }
00048 
00049                 File.Create(Context.Parameters["targetdir"] + "\\flag").Close();
00050 
00051                 UnpackDB();
00052                 using (log = new StreamWriter(file))
00053                 {
00054                     try
00055                     {
00056                         connectionString = string.Format("Data Source={0};Integrated Security=True;", Context.Parameters["db"]);
00057                         CreateDataBase();
00058                         log.WriteLine("CreateDataBase()");
00059                         AddUser();
00060                         log.WriteLine("AddUser()");
00061                         CorrectWebConfig();
00062                         log.WriteLine("CorrectWebConfig()");
00063                         InstallSelectedLanguage();
00064                         log.WriteLine("InstallSelectedLanguage()");
00065                     }
00066                     catch (Exception e)
00067                     {
00068                         log.WriteLine(e.Message);
00069                         log.WriteLine(e.StackTrace);
00070                         throw e;
00071                     }
00072                 }
00073             }
00074         }
00075 
00076         protected override void OnAfterUninstall(IDictionary savedState)
00077         {
00078             base.OnAfterUninstall(savedState);
00079             using (FileStream file = File.Open("c:\\log.txt", FileMode.OpenOrCreate))
00080             {
00081                 using (log = new StreamWriter(file))
00082                 {
00083                     File.Delete(Context.Parameters["targetdir"] + "\\RAIS");
00084                     File.Delete(Context.Parameters["targetdir"] + "\\flag");
00085                 }
00086             }
00087         }
00088 
00089         private void AddUser()
00090         {
00091             using (SqlConnection connection = new SqlConnection(connectionString))
00092             {
00093                 string commandText = string.Format(" IF EXISTS(select * from sys.syslogins where name = '{0}') DROP LOGIN [{0}]" +
00094                             "   CREATE LOGIN [{0}] WITH PASSWORD='{1}', CHECK_POLICY=OFF", Context.Parameters["login"],
00095                                         Context.Parameters["pass"],
00096                                         Context.Parameters["catalog"]);
00097                 connection.Open();
00098                 SqlCommand command = new SqlCommand(commandText, connection);
00099                 log.WriteLine(commandText);
00100                 command.ExecuteNonQuery();
00101             }
00102             string connectionString1 = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", Context.Parameters["db"], Context.Parameters["catalog"]);
00103             using (SqlConnection connection = new SqlConnection(connectionString1))
00104             {
00105                 string commandText = string.Format(" IF EXISTS(select * from sys.sysusers where name = '{0}') DROP USER [{0}] " +
00106                                 " CREATE USER [{0}] FOR LOGIN [{0}] WITH DEFAULT_SCHEMA = [dbo] " +
00107                                 " EXEC sp_addrolemember N'db_datareader', N'{0}' " +
00108                                 " EXEC sp_addrolemember N'db_datawriter', N'{0}' " +
00109                                 " EXEC sp_addrolemember N'db_owner', N'{0}' "
00110                                 , Context.Parameters["login"]);
00111                 connection.Open();
00112                 log.WriteLine(commandText);
00113                 SqlCommand command = new SqlCommand(commandText, connection);
00114                 command.ExecuteNonQuery();
00115             }
00116         }
00117 
00118         private void CorrectWebConfig()
00119         {
00120             string connectionStringConfig = string.Format("Data Source={0};Initial Catalog={1};Persist Security Info=True;User ID={2};Password={3};Min Pool Size=4;Max Pool Size=10;",
00121                 Context.Parameters["db"],
00122                 Context.Parameters["catalog"],
00123                 Context.Parameters["login"],
00124                 Context.Parameters["pass"]);
00125 
00126             XmlDocument webConfig = new XmlDocument();
00127             string webConfigFile = Context.Parameters["targetdir"] + "\\Web.config";
00128             webConfig.Load(webConfigFile);
00129 
00130             XmlNode configurationNode = webConfig.SelectSingleNode("//configuration");
00131 
00132             XmlNode connectionStringsNode = configurationNode.SelectSingleNode("//connectionStrings");
00133             if (connectionStringsNode == null)
00134                 log.WriteLine("//connectionStrings not exists");
00135 
00136             XmlNode addNode = connectionStringsNode.SelectSingleNode("//add[@name='RAISConnectionString']");
00137             if (addNode == null)
00138             {
00139                 log.WriteLine("//addNode not exists");
00140                 log.WriteLine(connectionStringsNode.InnerXml);
00141             }
00142 
00143             webConfig.SelectSingleNode("//configuration/connectionStrings/add[@name='RAISConnectionString']").Attributes["connectionString"].InnerText = connectionStringConfig;
00144             //webConfig.SelectSingleNode("//configuration/connectionStrings/add[@name='RADEVConnectionString']").Attributes["connectionString"].InnerText = connectionStringConfig;
00145 
00146             if (Context.Parameters["uploadfolder"] != "")
00147                 webConfig.SelectSingleNode("//appSettings/add[@key='UploadDirectory']").Attributes["value"].InnerText = Context.Parameters["uploadfolder"];
00148             webConfig.SelectSingleNode("//appSettings/add[@key='AdministratorEmailAccount']").Attributes["value"].InnerText = Context.Parameters["adminemail"];
00149             log.WriteLine(Context.Parameters["adminemail"]);
00150             log.WriteLine(Context.Parameters["uploadfolder"]);
00151             webConfig.Save(webConfigFile);
00152         }
00153 
00154         private void CreateDataBase()
00155         {
00156             string checkDataBaseCommand = string.Format("select * from sys.databases where [name]='{0}'", Context.Parameters["catalog"]);
00157 
00158             string restoreCommand =
00159                     " DECLARE @data_path nvarchar(256)," +
00160                     " @log_path nvarchar(256); " +
00161                     " EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'Software\\Microsoft\\MSSQLServer\\MSSQLServer', N'DefaultData', @data_path OUTPUT" +
00162                     " IF @data_path IS NULL BEGIN" +
00163                     "    EXEC master.dbo.xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\\Microsoft\\MSSQLServer\\Setup', N'SQLPath', @data_path OUTPUT" +
00164                     "    SET @data_path = @data_path + '\\Data\\'" +
00165                     " END " +
00166                     " ELSE BEGIN" +
00167                     "   SET @data_path = @data_path + '\\'" +
00168                     " END" +
00169 
00170                     " SET @log_path = @data_path + '{0}_log.LDF' " +
00171                     " SET @data_path = @data_path + '{0}.mdf' " +
00172 
00173                     " RESTORE DATABASE {0}" +
00174                     " FROM  DISK = '{1}\\RAIS'" +
00175                     " WITH  FILE = 1, " +
00176                     " NOUNLOAD, " +
00177                     " REPLACE, " +
00178                     " STATS = 10 " +
00179                     ",  MOVE N'RAIS' TO @data_path " +
00180                     ",  MOVE N'RAIS_Log' TO @log_path ";
00181 
00182 
00183             using (SqlConnection connection = new SqlConnection(connectionString))
00184             {
00185                 connection.Open();
00186                 string commandText = string.Format(restoreCommand, Context.Parameters["catalog"], Context.Parameters["targetdir"]);
00187                 SqlCommand command = new SqlCommand(checkDataBaseCommand, connection);
00188 
00189                 if (command.ExecuteScalar() != null)
00190                     throw new Exception(string.Format("The DB with name {0} exists on the server {1}. Please, enter an other DB name.", Context.Parameters["catalog"], Context.Parameters["db"]));
00191                 command.CommandTimeout = 240;
00192                 command.CommandText = commandText;
00193                 command.ExecuteNonQuery();
00194             }
00195             try
00196             {
00197                 File.Delete(Context.Parameters["targetdir"] + "\\RAIS");
00198             }
00199             catch (Exception) { }
00200         }
00201         private void UnpackDB()
00202         {
00203             int length;
00204             byte[] content;
00205             using (GZipStream compStream = new GZipStream(new FileStream(Context.Parameters["targetdir"] + "\\RAIS.zip", FileMode.Open, FileAccess.Read), CompressionMode.Decompress))
00206             {
00207                 length = 100000000;
00208                 content = new byte[length];
00209                 length = compStream.Read(content, 0, length);
00210             }
00211             using (FileStream fs = new FileStream(Context.Parameters["targetdir"] + "\\RAIS", FileMode.Create, FileAccess.Write))
00212                 fs.Write(content, 0, length);
00213         }
00214                 
00215         private void InstallSelectedLanguage()
00216         {
00217             string[][] Table = new string[50][];
00218             Table[1] = new string[] {"[Academic Degree]","[PK Academic Degree ID]","[Degree]"};
00219             Table[2] = new string[] {"[Action Type]","[PK Action Type ID]","[Action Type Name]"};
00220             Table[3] = new string[] {"[Asso Type]","[PK Asso Type ID]","[Type Name]"};
00221             Table[4] = new string[] {"[Authorization Status]","[PK Authorization Status ID]","[Authorization Status]"};
00222             Table[5] = new string[] {"[Boolean]","[PK Boolean ID]","[YesNo]"};
00223             Table[6] = new string[] {"[Branch]","[PK Branch ID]","[Name]"};
00224             Table[7] = new string[] {"[Consistency Check]","[PK Consistency Check ID]","[Message]"};
00225             Table[8] = new string[] {"[Country]","[PK Country ID]","[Country Name]"};
00226             Table[9] = new string[] {"[Deterministic Effects]","[PK Deterministic Effects ID]","[Deterministic Effects]"};
00227             Table[10] = new string[] {"[Dose Extremity Organ List]","[PK Dose Extremity Organ List ID]","[Extremity or Organ]"};
00228             Table[11] = new string[] {"[Dose Limits]","[PK Dose Limits ID]","[Limit Name]"};
00229             Table[12] = new string[] {"[Emergency Response Level]","[PK Emergency Response Level ID]","[Emergency Response Level]"};
00230             Table[13] = new string[] {"[Event Cause]","[PK Event Cause ID]","[Cause]"};
00231             Table[14] = new string[] {"[Extent of Event]","[PK Extent of Event ID]","[Extent of Event]"};
00232             Table[15] = new string[] {"[Facility Status]","[PK Facility Status ID]","[Status Name]"};
00233             Table[16] = new string[] {"[Field]","[PK Field ID]","[Name]"};
00234             Table[17] = new string[] {"[Generator Type]","[PK Generator Type ID]","[Type Name]"};
00235             Table[18] = new string[] {"[Health Consequences]","[PK Health Consequences ID]","[Health Consequences]"};
00236             Table[19] = new string[] {"[Inspection Status]","[PK Inspection Status ID]","[Inspection Status]"};
00237             Table[20] = new string[] {"[Monitoring Status]","[PK Monitoring Status ID]","[Status]"};
00238             Table[21] = new string[] {"[Non-Compliance]","[PK Non-Compliance ID]","[Name]"};
00239             Table[22] = new string[] {"[Non-Compliance Category]","[PK Non-Compliance Category ID]","[Category Name]"};
00240             Table[23] = new string[] {"[Non-Compliance Major Category]","[PK Non-Compliance Major Category ID]","[Major Category Name]"};
00241             Table[24] = new string[] {"[Officer Task]","[PK Officer Task ID]","[Officer Task]"};
00242             Table[25] = new string[] {"[Package Categories]","[PK Package Categories ID]","[Package Category Name]"};
00243             Table[26] = new string[] {"[Person Status]","[PK Person Status ID]","[Status]"};
00244             Table[27] = new string[] {"[Physical Barrier]","[PK Physical Barrier ID]","[Barrier Name]"};
00245             Table[28] = new string[] {"[Physical Form]","[PK Physical Form ID]","[Form]"};
00246             Table[29] = new string[] {"[Practice]","[PK Practice ID]","[Practice Name]"};
00247             Table[30] = new string[] {"[Practice Category]","[PK Practice Category ID]","[Category Name]"};
00248             Table[31] = new string[] {"[Professional Degree]","[PK Professional Degree ID]","[Degree Name]"};
00249             Table[32] = new string[] {"[Propagator - Consistency Checks]","[PK_SQL_ID]","[Message]"};
00250             Table[33] = new string[] {"[Radiological Concequences]","[PK Radiological Concequences ID]","[Event type description]"};
00251             Table[34] = new string[] {"[Regulatory Reference Status]","[PK Regulatory Reference Status ID]","[Status]"};
00252             Table[35] = new string[] {"[Regulatory Reference Type]","[PK Regulatory Reference Type ID]","[Type]"};
00253             Table[36] = new string[] {"[Report Type]","[PK Report Type ID]","[Report Type]"};
00254             Table[37] = new string[] {"[Response]","[PK Response ID]","[Name]"};
00255             Table[38] = new string[] {"[Sealed Category]","[PK Sealed Category ID]","[Category Name]"};
00256             Table[39] = new string[] {"[Security Group]","[PK Security Group ID]","[Name]"};
00257             Table[40] = new string[] {"[Sex]","[PK Sex ID]","[Sex]"};
00258             Table[41] = new string[] {"[Source Status]","[PK Source Status ID]","[Source Status]"};
00259             Table[42] = new string[] {"[System - Messages]","[PK_SQL_ID]","[Message Language Text]"};
00260             Table[43] = new string[] {"[System - Search]","[PK_SQL_ID]","[Search Item]"};
00261             Table[44] = new string[] {"[System - Search]","[PK_SQL_ID]","[Search Field]"};
00262             Table[45] = new string[] {"[Time Unit]","[PK Time Unit ID]","[Interval]"};
00263             Table[46] = new string[] {"[Transfer Type]","[PK Transfer Type ID]","[Transfer Type Name]"};
00264             Table[47] = new string[] {"[Transport Mode]","[PK Transport Mode ID]","[Transport Mode]"};
00265             Table[48] = new string[] {"[Treatment]","[PK Treatment ID]","[Treatment]"};
00266             Table[49] = new string[] {"[Wave Form]","[PK Wave Form ID]","[Wave Name]"};
00267 
00268             int SelectedLanguage = Convert.ToInt32(Context.Parameters["SelectedLanguage"]);
00269             string[] Language = new string[] { "Arabic", "English", "French", "Russian", "Spanish" };
00270 
00271             string connectionString1 = string.Format("Data Source={0};Initial Catalog={1};Integrated Security=True;", Context.Parameters["db"], Context.Parameters["catalog"]);
00272             using (SqlConnection connection = new SqlConnection(connectionString1))
00273             {
00274                 SqlCommand com = new SqlCommand("", connection);
00275                 SqlDataAdapter da = new SqlDataAdapter();
00276                 System.Data.DataTable dtLanguageValues = new System.Data.DataTable();
00277 
00278                 connection.Open();
00279 
00280                 com.Parameters.Add("@TableName", System.Data.SqlDbType.NVarChar).Value = "";
00281                 com.Parameters.Add("@PrimaryKey", System.Data.SqlDbType.Int).Value = 0;
00282                 com.Parameters.Add("@TranslationString", System.Data.SqlDbType.NVarChar).Value = "";
00283                 
00284                 for (int i = 1; i <= 49; i++)
00285                 {
00286                     com.CommandText = "SELECT [FK ID], " + Language[SelectedLanguage - 1] + " AS TranslationString " + "FROM [Translation Install Values] " + "WHERE [Table Name] = @TableName";
00287                     com.Parameters["@TableName"].Value = Table[i][0];
00288 
00289                     dtLanguageValues.Clear();
00290                     da.SelectCommand = com;
00291                     da.Fill(dtLanguageValues);
00292 
00293                     com.CommandText = "UPDATE " + Table[i][0] + " SET " + Table[i][2] + " = @TranslationString " + "WHERE " + Table[i][1] + " = @PrimaryKey";
00294                     
00295                     foreach (System.Data.DataRow row in dtLanguageValues.Rows)
00296                     {
00297                         if ((Table[i][0] == "[System - Search]") & (Table[i][2] == "[Search Field]"))
00298                            com.Parameters["@PrimaryKey"].Value = (int)row["FK ID"] - 100;
00299                         else
00300                             com.Parameters["@PrimaryKey"].Value = row["FK ID"];
00301                         com.Parameters["@TranslationString"].Value = row["TranslationString"];
00302                         com.ExecuteNonQuery();
00303                     }
00304                 }
00305             }
00306         }
00307     }
00308 }